/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.network.serverpackets;

import java.util.Calendar;

import javolution.util.FastList;

import jmmos.gameserver.instancemanager.TerritoryWarManager;
import jmmos.gameserver.instancemanager.TerritoryWarManager.Territory;
import jmmos.gameserver.model.MMOGuild;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;

/**
 * @author GodKratos
 */
public class ExShowDominionRegistry extends MMOGameServerPacket
{
	private static final int MINID = 80;
	private final int _castleId;
	private int _guildReq = 0x00;
	private int _mercReq = 0x00;
	private int _isMercRegistered = 0x00;
	private int _isGuildRegistered = 0x00;
	private int _warTime = (int) (Calendar.getInstance().getTimeInMillis() / 1000);
	private final int _currentTime = (int) (Calendar.getInstance().getTimeInMillis() / 1000);
	
	public ExShowDominionRegistry(int castleId, MMOPcInstance player)
	{
		_castleId = castleId;
		if (TerritoryWarManager.getInstance().getRegisteredGuilds(castleId) != null)
		{
			_guildReq = TerritoryWarManager.getInstance().getRegisteredGuilds(castleId).size();
			if (player.getGuild() != null)
			{
				_isGuildRegistered = (TerritoryWarManager.getInstance().getRegisteredGuilds(castleId).contains(player.getGuild()) ? 0x01 : 0x00);
			}
		}
		if (TerritoryWarManager.getInstance().getRegisteredMercenaries(castleId) != null)
		{
			_mercReq = TerritoryWarManager.getInstance().getRegisteredMercenaries(castleId).size();
			_isMercRegistered = (TerritoryWarManager.getInstance().getRegisteredMercenaries(castleId).contains(player.getObjectId()) ? 0x01 : 0x00);
		}
		_warTime = (int) (TerritoryWarManager.getInstance().getTWStartTimeInMillis() / 1000);
	}
	
	@Override
	protected void writeImpl()
	{
		writeC(0xfe);
		writeH(0x90);
		writeD(MINID + _castleId); // Current Territory Id
		if (TerritoryWarManager.getInstance().getTerritory(_castleId) == null)
		{
			// something is wrong
			writeS("No Owner"); // Owners Guild
			writeS("No Owner"); // Owner Guild Leader
			writeS("No Ally"); // Owner Alliance
		}
		else
		{
			MMOGuild guild = TerritoryWarManager.getInstance().getTerritory(_castleId).getOwnerGuild();
			if (guild == null)
			{
				// something is wrong
				writeS("No Owner"); // Owners Guild
				writeS("No Owner"); // Owner Guild Leader
				writeS("No Ally"); // Owner Alliance
			}
			else
			{
				writeS(guild.getName()); // Owners Guild
				writeS(guild.getLeaderName()); // Owner Guild Leader
				writeS(guild.getAllyName()); // Owner Alliance
			}
		}
		writeD(_guildReq); // Guild Request
		writeD(_mercReq); // Merc Request
		writeD(_warTime); // War Time
		writeD(_currentTime); // Current Time
		writeD(_isGuildRegistered); // is Cancel guild registration
		writeD(_isMercRegistered); // is Cancel mercenaries registration
		writeD(0x01); // unknown
		FastList<Territory> territoryList = TerritoryWarManager.getInstance().getAllTerritories();
		writeD(territoryList.size()); // Territory Count
		for (Territory t : territoryList)
		{
			writeD(t.getTerritoryId()); // Territory Id
			writeD(t.getOwnedWardIds().size()); // Emblem Count
			for (int i : t.getOwnedWardIds())
			{
				writeD(i); // Emblem ID - should be in for loop for emblem count
			}
		}
	}
}
